﻿using System;
using System.Application;
using System.Collections.Generic;
using System.Domain.Entity;
using System.Domain.IRepository;
using System.Linq;
using System.Repository;
using System.Repository.Base;
using System.Text;
using System.Threading.Tasks;
using Application.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Ubiquity.Core.Extend;
using Ubiquity.Web.Filters;

namespace Ubiquity.Web.Controllers
{
    public class ClientsDataController : BaseController
    {
         
        private readonly SysRoleAuthorizeService _roleAuthorizeService;
        private readonly SysRoleService _roleApp;
        public ClientsDataController(SysRoleAuthorizeService roleAuthorizeService, SysRoleService roleApp)
        {
            _roleAuthorizeService = roleAuthorizeService;
            _roleApp = roleApp;
        }

        [HttpGet] 
        public ActionResult GetClientsDataJson()
        {
            var data = new
            {
                role = GetRoleList(),
                user = "",
                authorizeMenu = GetMenuList(),
                authorizeButton = GetMenuButtonList(),
            };
            return Content(data.ToJson());
        }

        private object GetRoleList()
        {
         
            var data = _roleApp.GetList();
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (SysRoleEntity item in data)
            {
                var fieldItem = new
                {
                    encode = item.EnCode,
                    fullname = item.FullName
                };
                dictionary.Add(item.Id, fieldItem);
            }
            return dictionary;
        }
        /// <summary>
        /// 获取用户的 菜单
        /// </summary>
        /// <returns></returns>
        private object GetMenuList()
        {
            //var roleId = OperatorProvider.Provider.GetCurrently(LoginManage.GetLoginId()).RoleId;
            var roleId = "508baa01-b558-4bdc-9008-9e0e90a3ef2c";//管理员
            string temp = ToMenuJson(_roleAuthorizeService.GetMenuList(roleId).OrderBy(t => t.SortCode).ToList(), "0");
            return temp;

        }
        /// <summary>
        /// 菜单列表Json
        /// </summary>
        /// <param name="data"></param>
        /// <param name="parentId">父级ID</param>
        /// <returns></returns>

        private string ToMenuJson(List<SysModuleEntity> data, string parentId)
        {
            StringBuilder sbJson = new StringBuilder();
            sbJson.Append("[");
            List<SysModuleEntity> entitys = data.FindAll(t => t.ParentId == parentId);
            if (entitys.Count > 0)
            {
                foreach (var item in entitys.OrderBy(t => t.SortCode))
                {
                    if (item.Target.Equals("expand") || Convert.ToBoolean(item.IsMenu) && Convert.ToBoolean(item.EnabledMark))
                    {
                        string strJson = item.ToJson();
                        strJson = strJson.Insert(strJson.Length - 1, ",\"ChildNodes\":" + ToMenuJson(data, item.Id) + "");
                        sbJson.Append(strJson + ",");
                    }
                }
                sbJson = sbJson.Remove(sbJson.Length - 1, 1);
            }
            sbJson.Append("]");
            return sbJson.ToString();


        }
        /// <summary>
        /// 得到按钮集合
        /// </summary>
        /// <returns></returns>
        private object GetMenuButtonList()
        {
            //var roleId = OperatorProvider.Provider.GetCurrently(LoginManage.GetLoginId()).RoleId;
            var roleId = "508baa01-b558-4bdc-9008-9e0e90a3ef2c";//管理员
            var data = _roleAuthorizeService.GetButtonList(roleId);
            var dataModuleId = data.Distinct(new ExtList<SysModuleAuthorizeEntity>("ModuleId"));
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (var item in dataModuleId)
            {
                //   foreach (var item in data) { 
                var buttonList = data.Where(t => t.ModuleId.Equals(item.ModuleId));
                dictionary.Add(item.ModuleId, buttonList);
            }
            return dictionary;
        }
    }
}